package com.wjm.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wjm.entity.Building;
import com.wjm.entity.Classroom;
import com.wjm.entity.Desk;
import com.wjm.service.impl.BuildingServiceImpl;
import com.wjm.util.CacheUtil;
import com.wjm.util.StringUtil;

@Controller
@RequestMapping("/web/building")
public class BuildingWebController {

	@Resource(name = "buildingService")
	private BuildingServiceImpl buildingService;

	private final String listKey = "list";
	private final String pagesKey = "pages";
	private final String pageKey = "page";
	private final String statusKey = "status";

	@RequestMapping("/query")
	public String query(int page, int status, Model model) {
		String result = "";
		try {
			int total = buildingService.queryTotal(status);
			int number = Integer.parseInt(CacheUtil.getProperties().getProperty(StringUtil.BUILDING_NUM));
			int pages = total % number == 0 ? (total / number) : (total / number + 1);
			System.out.println(pages);
			List<Building> list = null;
			if (page <= pages) {
				list = buildingService.queryByPage(page, number, status);
			} else {
				list = new ArrayList<Building>();
			}
			model.addAttribute(listKey, list);
			model.addAttribute(pageKey, page);
			model.addAttribute(pagesKey, pages);
			model.addAttribute(statusKey, status);
			result = "/web/building/building-show";
		} catch (Exception e) {
			e.printStackTrace();
			result = "/error";
		}
		return result;
	}

	@RequestMapping("/updateStatus")
	@ResponseBody
	public String update(int buId, int status) {
		String result = "";
		try {
			Building building = new Building();
			building.setBuId(buId);
			building.setBuStatus(status);
			if ((status == 1 || status == 2) && buildingService.update(building)) {
				
				// 同时修改缓存中的状态
				CacheUtil.getBuildings().get(buId).setBuStatus(status);
				for (Classroom room : CacheUtil.getUnuseroom().get(buId)) {
					room.setCrStatus(status);
					for(Desk desk : CacheUtil.getDesks().get(room.getCrId())){
						desk.setDeskStatus(status);
					}
				}
				result = "{\"result\":\"success\"}";
			} else {
				result = "{\"result\":\"fail\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"result\":\"error\"}";
		}
		return result;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String delete(int buId) {
		String result = "";
		try {
			if (buId > 0 && buildingService.delete(buId)) {

				// 将缓存中的信息删除
				CacheUtil.getBuildings().remove(buId);
				for (Classroom room : CacheUtil.getUnuseroom().get(buId)) {
					CacheUtil.getDesks().remove(room.getCrId());
				}
				CacheUtil.getUnuseroom().remove(buId);
				for (Classroom room : CacheUtil.getUseroom().get(buId)) {
					CacheUtil.getDesks().remove(room.getCrId());
				}
				CacheUtil.getUseroom().remove(buId);

				result = "{\"result\":\"success\"}";
			} else {
				result = "{\"result\":\"fail\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"result\":\"error\"}";
		}
		return result;
	}

	@RequestMapping("/add")
	@ResponseBody
	public String add(Building building) {
		String result = "";
		try {
			if (buildingService.save(building)) {
				building.setBuStatus(1);
				// 缓存中添加
				CacheUtil.getBuildings().put(building.getBuId(), building);
				result = "{\"result\":\"success\"}";
			} else {
				result = "{\"result\":\"fail\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"result\":\"error\"}";
		}
		return result;
	}
}
